package com.bluestation.net.http;

/*****************************************************************
 *
 * Author: Partha Kuchana
 * Date : 23rd August 2001
 * Description: This class has methods used by other classes in the 
 * Web Server application.
 *
 * WSUtil.java	1.0 23/08/2001 Partha Kuchana
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this block appears in all copies. 
 *
 * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 ******************************************************************/

import java.net.*;
import java.io.*;
import java.util.*;

public class WSUtil
{
	@SuppressWarnings("unused")
	private Socket				socket	= null;
	@SuppressWarnings("unused")
	private PrintStream			pw		= null;
	@SuppressWarnings("unused")
	private DataOutputStream	DOS		= null;
	@SuppressWarnings("unused")
	private BufferedReader		in		= null;

	/**
	 * This method returns the current date,time
	 * @return String - Current Date, time
	 */

	private static String getCurrentDate()
	{
		String currentDate = "";

		Calendar c = Calendar.getInstance();

		String yyyy = new Integer(c.get(Calendar.YEAR)).toString();
		String mm = new Integer(c.get(Calendar.MONTH) + 1).toString();
		String dd = new Integer(c.get(Calendar.DATE)).toString();
		String hr = new Integer(c.get(Calendar.HOUR)).toString();
		String min = new Integer(c.get(Calendar.MINUTE)).toString();

		if (mm.trim().length() == 1)
		{
			mm = "0" + mm;
		}

		if (dd.trim().length() == 1)
		{
			dd = "0" + dd;
		}

		currentDate = mm + "/" + dd + "/" + yyyy + " - " + hr + ":" + min;

		return currentDate;
	}

	/**
	 * This method writes the given text to the specified file. 
	 * The file is created, if it does not exist.
	 * @param contentToWrite java.lang.String
	 * @return boolean - true, if the append is successful. false, otherwise.
	 */
	public static boolean log(String contentToWrite)
	{
		boolean result = true;

		try
		{
			contentToWrite = WSUtil.getCurrentDate() + " - " + contentToWrite
					+ "\n\n";
			DataOutputStream dos = new DataOutputStream(new FileOutputStream(
					WebServer.LOG_FILE, true));
			dos.writeBytes(contentToWrite);
			dos.close();
		}
		catch (Exception e)
		{
			result = false;
		}

		return result;
	}
}
